/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Drawing;

using SdlDotNet.Graphics;
using SdlDotNet.Input;
using SdlDotNet.Core;

using BlindShark.Widgets;

namespace BlindShark.GameStates
{
	public class GS_SelectLanguage : GameState
	{
		ListBox _lsbLanguages;
		CheckBox _chkNoAskAnymore;
		Label _lblNoAskAnymore;
		Button _btnOK;
		
		public GS_SelectLanguage()
		{
			string _sFont = "fonts/Fertigo_17b.otf";
			Label.DefaultFont = _sFont;
			
			// Initializing the ListBox
			_lsbLanguages = new ListBox(250, 47);
			_lsbLanguages.SelectionChanged += new ListBox.SelectionChangedEvent(SelectLanguage);
			
			// Initializing the CheckBox and the Label
			_chkNoAskAnymore = new CheckBox("gfx/checkBoxes2.jpg");
			
			_lblNoAskAnymore = new Label("", 22, Color.Black, TextAlignment.CenterLeft);
			
			_btnOK = new Button("gfx/buttons1.jpg", true);
			_btnOK.Font = _sFont;
			_btnOK.Text = "OK";
			_btnOK.TextColor = Color.Orange;
			_btnOK.TextSize = 20;
			_btnOK.Click += new Button.ClickEvent(Validate);
		}
		
		private void LoadLanguages()
		{
			// Going through the "languages" directory
			System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("languages");
			if (! directory.Exists)
			{
				// TODO: launching an error
			}
			foreach(System.IO.FileInfo file in directory.GetFiles("*.lng"))
			{
				Language l;
				l = new Language(file.Name);
				_lsbLanguages.AddItem(l);
			}
		}
		
		#region GameState interface members
		public void Draw()
		{
			ScreenManager.MainScreen.Fill(Color.SaddleBrown);
			
			// Displaying the listbox
			ScreenManager.MainScreen.Blit(_lsbLanguages.Surface, _lsbLanguages.Location);
			
			// Displaying the Checkbox
			if (!_lblNoAskAnymore.Text.Equals(""))
			{
				ScreenManager.MainScreen.Blit(_chkNoAskAnymore.Surface, _chkNoAskAnymore.Location);
				ScreenManager.MainScreen.Blit(_lblNoAskAnymore.Surface, _lblNoAskAnymore.Location);
				ScreenManager.MainScreen.Blit(_btnOK.Surface, _btnOK.Location);
			}
			
		}
		
		public void CleanUp()
		{
			_lsbLanguages.CleanUp();
			_chkNoAskAnymore.CleanUp();
			_btnOK.CleanUp();
		}
		
		public void Init()
		{	
			LoadLanguages();
			// Initializing the ListBox
			_lsbLanguages.Init();
			_lsbLanguages.Location = new Point((ScreenManager.Width - _lsbLanguages.Width) / 2,
			                                   (ScreenManager.Height - _lsbLanguages.Height) / 2);
			
			// Initializing the Checkbox
			_chkNoAskAnymore.Init();
			_chkNoAskAnymore.Location = new Point(10, ScreenManager.Height - 10 - _chkNoAskAnymore.Height);
			// Initializing the label
			_lblNoAskAnymore.Location = new Point(_chkNoAskAnymore.Right + 10, _chkNoAskAnymore.Y);
			_lblNoAskAnymore.Size = new Size(ScreenManager.Width - _chkNoAskAnymore.Width - 20, _chkNoAskAnymore.Height);
			_lblNoAskAnymore.Init();
			
			// Initializing the Button
			_btnOK.Init();
			_btnOK.Enabled = false;
			_btnOK.X = ScreenManager.Width - 10 - _btnOK.Width;
			_btnOK.Y = ScreenManager.Height - 10 - _btnOK.Height;
		}
		
		public void HandleEvents()
		{
			while(Events.Poll())
			{
				_lsbLanguages.HandleEvents();
				_chkNoAskAnymore.HandleEvents();
				_btnOK.HandleEvents();
			}
		}
		
		public void Update()
		{
			
		}
		#endregion
		
		public void SelectLanguage()
		{
			Language lang = _lsbLanguages.SelectedItem as Language;
			LanguageManager.Language = lang.ID;
			
			_lblNoAskAnymore.Text = LanguageManager.Get("NO_ASK");
			_lblNoAskAnymore.Init();
			
			_btnOK.Enabled = true;
		}
		
		public void Validate()
		{
			// Setting the language
			Language lang = _lsbLanguages.SelectedItem as Language;
			LanguageManager.Language = lang.ID;
			
			// Registering the No Ask Anymore choice
			LanguageManager.ChooseLanguage = false;
			GameManager.SaveConfiguration();
			
			GameStateManager.ChangeState(new GS_TitleScreen());
		}
	}
}
